package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.YonbipTokenUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.OrgDept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrgOrgs;
import com.ruoyi.system.service.IOrgOrgsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * yonbip组织Controller
 * 
 * @author ruoyi
 * @date 2025-09-05
 */
@Controller
@RequestMapping("/system/orgOrgs")
public class OrgOrgsController extends BaseController
{
    private String prefix = "system/orgOrgs";


    private IOrgOrgsService orgOrgsService = SpringUtils.getBean(IOrgOrgsService.class);


    @RequiresPermissions("system:orgOrgs:view")
    @GetMapping()
    public String orgOrgs()
    {
        return prefix + "/orgOrgs";
    }

    /**
     * 查询yonbip组织列表
     */

    @RequiresPermissions("system:orgOrgs:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrgOrgs orgOrgs)
    {
        ApiQueryOrg();
        startPage();
        List<OrgOrgs> list = orgOrgsService.selectOrgOrgsList(orgOrgs);
        return getDataTable(list);
    }

    /**
     * 导出yonbip组织列表
     */
    @RequiresPermissions("system:orgOrgs:export")
    @Log(title = "yonbip组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrgOrgs orgOrgs)
    {
        List<OrgOrgs> list = orgOrgsService.selectOrgOrgsList(orgOrgs);
        ExcelUtil<OrgOrgs> util = new ExcelUtil<OrgOrgs>(OrgOrgs.class);
        return util.exportExcel(list, "yonbip组织数据");
    }

    /**
     * 新增yonbip组织
     */
    @RequiresPermissions("system:orgOrgs:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存yonbip组织
     */
    @RequiresPermissions("system:orgOrgs:add")
    @Log(title = "yonbip组织", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrgOrgs orgOrgs)
    {
        return toAjax(orgOrgsService.insertOrgOrgs(orgOrgs));
    }

    /**
     * 修改yonbip组织
     */
    @RequiresPermissions("system:orgOrgs:edit")
    @GetMapping("/edit/{pkOrg}")
    public String edit(@PathVariable("pkOrg") String pkOrg, ModelMap mmap)
    {
        OrgOrgs orgOrgs = orgOrgsService.selectOrgOrgsByPkOrg(pkOrg);
        mmap.put("orgOrgs", orgOrgs);
        return prefix + "/edit";
    }

    /**
     * 修改保存yonbip组织
     */
    @RequiresPermissions("system:orgOrgs:edit")
    @Log(title = "yonbip组织", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrgOrgs orgOrgs)
    {
        return toAjax(orgOrgsService.updateOrgOrgs(orgOrgs));
    }

    /**
     * 删除yonbip组织
     */
    @RequiresPermissions("system:orgOrgs:remove")
    @Log(title = "yonbip组织", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orgOrgsService.deleteOrgOrgsByPkOrgs(ids));
    }

    public void ApiQueryOrg(){
        YonbipTokenUtil yonbipTokenUtil= new YonbipTokenUtil();
        // 获取token

        // System.out.println(token);
        // 请求的OpenAPI接口地址，注意nccloud前面没有"/"
        String apiuri = "nccloud/api/riaorg/orgmanage/org/queryOrgByEnablestate";

        JSONObject jsonObject = new JSONObject();


        jsonObject.set("enablestate", "2");
        String requestParam =jsonObject.toString();
        // 携带token访问接口，获取结果
        String result = null;
        List<String> lines=new ArrayList<>();
        try
        {
            result = yonbipTokenUtil.callAPI(apiuri, requestParam);
            JSONObject jsonObjectResult = JSONUtil.parseObj(result);

            List<OrgOrgs> orgOrgsList= JSONUtil.toList(jsonObjectResult.getStr("data"),OrgOrgs.class);

                for(OrgOrgs orgOrgs: orgOrgsList){
                    orgOrgsService.insertOrgOrgs(orgOrgs);
               }

//            YonbipFileUtils.createFileWithLines(
//                    "C:/example_directory",
//                    "multi_line.txt",
//                    lines,
//                    false
//            );

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(result);
    }
}
